//
//  NativeRouter.m
//  huien
//
//  Created by yiban on 2017/7/5.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "NativeRouter.h"
#import "Html5ViewController.h"
#import "HEFriendsViewController.h"
#import "UserLoginController.h"
#import "NSString+YBAdd.h"
#import "HeCertificationViewController.h"
#import "HEFriendsViewController.h"
#import "AppDelegate+HE.h"
#import "HEMeViewController.h"
#import "HEInvestViewController.h"
#import "HEMainViewController.h"
@interface NativeRouter ()<LcbLoginDelegate>
@property(nonatomic,strong)UIViewController*controller;
@property(nonatomic,strong)LcbBaseModel *baseModel;
@end

@implementation NativeRouter
LcbSingletonM(NativeRouter)
@synthesize baseModel=_baseModel;
-(BOOL)isCertification:(UIViewController*)controller
{
    self.controller= controller;
    SessionModel *session = [HEShare sessionModel];
    if (session.session_user.cardId!=nil||session.session_user.cardId.length>0) {
        return YES;
    }
    HeCertificationViewController *certification =[[HeCertificationViewController alloc] init];
    [self.controller.navigationController pushViewController:certification animated:YES];
    return NO;
}
-(BOOL)isLogin:(UIViewController*)controller
{
    self.controller= controller;
    if ([HEShare isLogin]==NO) {
        if ([HEShare isGestureOpen]) {
            UserLoginController *userlogin =[UserLoginController newWithGestureDelegate:self];
            [controller presentViewController:userlogin animated:YES completion:nil];
            
        }else{
            UserLoginController *viewcontroller =[UserLoginController newWithLoginDelegate:self];
            [controller presentViewController:viewcontroller animated:NO completion:nil];
        }
        return NO;
    }
    return YES;
}
- (void)userLoginOK:(id)userinfo
{
    if ([userinfo isKindOfClass:[SessionModel class]]) {
        SessionModel *sessionModel = (SessionModel*)userinfo;
        [[JGShare sharedJGShare] setPushAlias:[NSString stringWithFormat:@"%ld",sessionModel.session_user.userId]];
        [[LcbCache sharedCache] setObject:sessionModel forKey:@"SessionModel"];
    }
    [self.controller dismissViewControllerAnimated:YES completion:nil];
}
- (void)userLoginFail
{
    if ([self.controller isKindOfClass:[HEInvestViewController class]]||[self.controller isKindOfClass:[HEMeViewController class]]) {
        AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [appDelegate setSelectTable:0];
    }
    [self.controller dismissViewControllerAnimated:YES completion:nil];
}
- (void)userDidCancel
{
    if ([self.controller isKindOfClass:[HEInvestViewController class]]||[self.controller isKindOfClass:[HEMeViewController class]]) {
        AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [appDelegate setSelectTable:0];
    }
    [self.controller dismissViewControllerAnimated:YES completion:nil];
}

-(void)gotoH5WithVC:(UIViewController*)controller title:(NSString*)title linkurl:(NSString*)link
{
    if (link==nil||link.length==0) {
        return;
    }
    Html5ViewController *h5 =[[Html5ViewController alloc] initWithUrlString:link titleString:title];
    [controller.navigationController pushViewController:h5 animated:YES];
}

-(void)nativeRouter:(UIViewController*)controller params:(id)param completion:(void(^)(id))completion
{
    self.controller= controller;
    NSDictionary *dict = [LcbJson objectToDictionary:param];
//    NSLog(@"-----%@",dict);
    if ([[dict allKeys] containsObject:@"action"]) {
        NSString *action = [dict objectForKey:@"action"];
        if ([action containsString:@"/mobileLoan/loans"] || [action containsString:@"/mobileDebt/debts"]) {
            if ([self isLogin:self.controller])
            {
                if ([action containsString:@"/mobileDebt/debts"]) {
                    [HEShare sharedHEShare].selectedIndex=1;
                }else{
                    [HEShare sharedHEShare].selectedIndex=0;
                }
                AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
                [appDelegate setSelectTable:1];
    
                return;
            }
        }else if([action containsString:@"/mobileVisitor/getPageHtml?name=新手指引"]||[action containsString:@"/mobileVisitor/getHuienHtml?name=关于惠恩"])//邀请好友
        {
            [self gotoH5WithVC:self.controller title:[dict objectForKey:@"title"]  linkurl:[NSString stringWithFormat:@"%@%@",[LcbDevice sharedLcbDevice].domain,action]];
        }else
        if([action containsString:@"mobileMoreIntroduc/get_promote_links"])//邀请好友
        {
            if ([self isLogin:self.controller]) {
                if ([self isCertification:self.controller]) {
                    HEFriendsViewController *friend = [[HEFriendsViewController alloc] init];
                    [self.controller.navigationController pushViewController:friend animated:YES];
                }
                return;
            }
        }else
        {
            if ([self isLogin:self.controller]) {
                if ([self isCertification:self.controller]) {
                    [self gotoH5WithVC:self.controller title:[dict objectForKey:@"title"]  linkurl:[NSString stringWithFormat:@"%@%@",[LcbDevice sharedLcbDevice].domain,action]];
                }
                return;
            }
            
           
        }

    }else
    {
       [self gotoH5WithVC:controller title:[dict objectForKey:@"title"] linkurl:[dict objectForKey:@"linkUrl"]];
    }
    
}

-(void)codeHander:(UIViewController*)controller params:(id)param completion:(void(^)(id))completion
{
    self.controller= controller;
    LcbBaseModel *basemodel = (LcbBaseModel*)param;
    self.baseModel = basemodel;
    switch (basemodel.code) {
        case 11:
        case 12:
        case 13:
        {
            //账号类错误
            //11	账号不能为空！
            //12	帐号被禁用了，请联系系统管理员！
            //13	帐号故障，请联系系统管理员！
            [YBToast showWithText:basemodel.message];
            
        }
            break;
        case 21:
        case 22:
        case 23:
        case 24:
        case 25:
        case 26:
        case 27:
        case 28:
        case 29:
        {
            [YBToast showWithText:basemodel.message];
            //密码类错误
            //21	密码不能为空！
            //22	用户名或密码错误！
            //23	您输入的密码格式不正确，请重新长度未6~25个字符的密码！
            //24	新密码不能为空！
            //25	确认密码不能为空！
            //26	新密码长度不正确，请重新输入长度为6~25个字符的密码！
            //27	确认密码长度不正确，请重新输入长度为6~25个字符的密码！
            //28	新密码和确认密码不一致，请重新输入！
            //29	原始密码不正确！

        }
            break;
        case 31:
        case 32:
        case 33:
        case 34:
        {
            //手机号错误
            //31	手机号不能为空！
            //32	您输入的手机格式不正确，请重新输入！
            //33	您输入的手机号已被注册，请重新输入或前往登录！
            //34	您输入的手机号未注册，请重新输入或前往注册！
            [YBToast showWithText:basemodel.message];
        }
            break;
        case 41:
        case 42:
        case 43:
        case 44:
        case 45:
        {
            //验证码错误
            //41	手机验证码不能为空！
            //42	验证码已发送，有效时间为30分钟，请勿重复提交！
            //43	验证码发送失败，请稍后尝试！
            //44	您输入的手机验证码不正确，请重新输入！
            //45	您输入的手机验证码不存在，请重新获取！
            [YBToast showWithText:basemodel.message];
        }
            break;
        case 51:
        {
            //邀请码错误
            //51	您输入的邀请码不正确，请核对后再提交！
            [YBToast showWithText:basemodel.message];
        }
            break;
        case 61:
        {
            //实名认证错误
            //61	为提升账户安全性，方便发放活动奖励成为惠恩资本优质客户，快去实名认证吧！
            HeCertificationViewController *certification =[[HeCertificationViewController alloc] init];
            [controller.navigationController pushViewController:certification animated:YES];
            return;
        }
            break;
        case 62:
        case 63:
        case 64:
        case 65:
        {
            //实名认证错误
            //61	为提升账户安全性，方便发放活动奖励成为惠恩资本优质客户，快去实名认证吧！
            //62	您输入的真实姓名不能为空！
            //63	您输入的身份证号不能为空！
            //64	您输入的身份证号格式不正确，请重新输入！
            //65	您输入的身份证号已被认证，请确认后再次输入！
            [YBToast showWithText:basemodel.message];

        }
            break;
        case 71:
        {
            //未开通汇付
            //71	为提升账户安全性，方便发放活动奖励成为惠恩资本优质客户，快去开通资金托管吧！
            UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:basemodel.data[@"title"] message:basemodel.message delegate:self cancelButtonTitle:@"暂不开通" otherButtonTitles:@"开通", nil];
            [alertView show];
            
        }
            break;
        case 81:
        {
            //未绑卡错误:
            //81	为提升账户安全性，方便发放活动奖励成为惠恩资本优质客户，快去绑定银行卡吧！
            UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:basemodel.data[@"title"] message:basemodel.message delegate:self cancelButtonTitle:@"暂不绑定" otherButtonTitles:@"绑定", nil];
            [alertView show];
            
        }
            break;
        case 101:
        {
            //公共错误
            //101	未登录，请先登录！
            if ([HEShare isGestureOpen]) {
                UserLoginController *userlogin =[UserLoginController newWithGestureDelegate:self];
                [controller presentViewController:userlogin animated:YES completion:nil];
            }else{
                UserLoginController *viewcontroller =[UserLoginController newWithLoginDelegate:self];
                [controller presentViewController:viewcontroller animated:NO completion:nil];
            }
            return;
        }
            break;
        case 102:
        {
            //公共错误
            //102	请求失败，请稍后尝试！
            [YBToast showWithText:basemodel.message];
            AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [appDelegate setSelectTable:0];
            return;
        }
            break;
        default:
            break;
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            [HEShare sharedHEShare].authStatus = 1;
             [[NSNotificationCenter defaultCenter] postNotificationName:@"authStatus" object:nil];
        }
            break;
        case 1:
        {
            [self gotoH5WithVC:self.controller title:self.baseModel.data[@"title"] linkurl:[NSString stringWithFormat:@"%@%@",[LcbDevice sharedLcbDevice].domain,self.baseModel.data[@"action"] ]];
        }
            break;
        default:
            break;
    }
}
@end
